package com.hd.tiyu.yuejian;


import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hd.tiyu.yuejian.Fragment.JulebuFragment;
import com.hd.tiyu.yuejian.Fragment.PeixunFragment;
import com.hd.tiyu.yuejian.Fragment.ShaituFragment;
import com.hd.tiyu.yuejian.Fragment.ShouyeFragment;
import com.hd.tiyu.yuejian.Fragment.WoFragment;


public class MainActivity extends FragmentActivity implements OnClickListener {
    private LinearLayout ShouYe;
    private LinearLayout PeiXun;
    private LinearLayout JuLeBu;
    private LinearLayout ShaiTu;
    private LinearLayout Wo;

    private ImageView mImgShouye;
    private ImageView mImgPeixun;
    private ImageView mImgJulebu;
    private ImageView mImgShaitu;
    private ImageView mImgWo;

    private TextView Textpeixun;
    private TextView Textjulebu;
    private TextView Textshaitu;
    private TextView Textwo;

    private Fragment TabShouye;
    private Fragment TabPeixun;
    private Fragment TabJulebu;
    private Fragment TabShaitu;
    private Fragment TabWo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);
    }
    private void initEvent(){
        ShouYe.setOnClickListener(this);
        PeiXun.setOnClickListener(this);
        JuLeBu.setOnClickListener(this);
        ShaiTu.setOnClickListener(this);
        Wo.setOnClickListener(this);
    }
    private void initView(){
        ShouYe=(LinearLayout)findViewById(R.id.id_tab_ShouYe);
        PeiXun=(LinearLayout)findViewById(R.id.id_tab_PeiXun);
        JuLeBu=(LinearLayout)findViewById(R.id.id_tab_JuLeBu);
        ShaiTu=(LinearLayout)findViewById(R.id.id_tab_ShaiTu);
        Wo=(LinearLayout)findViewById(R.id.id_tab_Wo);

        Textpeixun=findViewById(R.id.peixun);
        Textjulebu=findViewById(R.id.julebu);
        Textshaitu=findViewById(R.id.shaitu);
        Textwo=findViewById(R.id.wo);

        mImgShouye=(ImageView)findViewById(R.id.img_shouye);
        mImgPeixun=(ImageView)findViewById(R.id.img_peixun);
        mImgJulebu=(ImageView)findViewById(R.id.img_julebu);
        mImgShaitu=(ImageView)findViewById(R.id.img_shaitu);
        mImgWo=(ImageView)findViewById(R.id.img_wo);
    }
    @TargetApi(Build.VERSION_CODES.M)
    private void setSelect(int i){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(TabShouye==null){
                    TabShouye=new ShouyeFragment();
                    transaction.add(R.id.id_content,TabShouye);
                }else {
                    transaction.show(TabShouye);
                }
                mImgShouye.setImageResource(R.drawable.ic_shouye1);
                break;
            case 1:
                if(TabPeixun==null){
                    TabPeixun=new PeixunFragment();
                    transaction.add(R.id.id_content,TabPeixun);
                }else {
                    transaction.show(TabPeixun);
                }
                mImgPeixun.setImageResource(R.drawable.ic_peixun1);
                Textpeixun.setTextColor(getColor(R.color.lv));
                break;
            case 2:
                if(TabJulebu==null){
                    TabJulebu=new JulebuFragment();
                    transaction.add(R.id.id_content,TabJulebu);
                }else {
                    transaction.show(TabJulebu);
                }
                mImgJulebu.setImageResource(R.drawable.ic_julebu1);
                Textjulebu.setTextColor(getColor(R.color.lv));
                break;
            case 3:
                if(TabShaitu==null){
                    TabShaitu=new ShaituFragment();
                    transaction.add(R.id.id_content,TabShaitu);
                }else {
                    transaction.show(TabShaitu);
                }
                mImgShaitu.setImageResource(R.drawable.ic_shaitu1);
                Textshaitu.setTextColor(getColor(R.color.lv));
                break;
            case 4:
                if(TabWo==null){
                    TabWo=new WoFragment();
                    transaction.add(R.id.id_content,TabWo);
                }else {
                    transaction.show(TabWo);
                }
                mImgWo.setImageResource(R.drawable.ic_wo1);
                Textwo.setTextColor(getColor(R.color.lv));
                break;
        }
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){
        if(TabShouye!=null){
            transaction.hide(TabShouye);
        }
        if(TabPeixun!=null){
            transaction.hide(TabPeixun);
        }
        if(TabJulebu!=null){
            transaction.hide(TabJulebu);
        }
        if(TabShaitu!=null){
            transaction.hide(TabShaitu);
        }
        if(TabWo!=null){
            transaction.hide(TabWo);
        }
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.id_tab_ShouYe:
                setSelect(0);
                break;
            case R.id.id_tab_PeiXun:
                setSelect(1);
                break;
            case R.id.id_tab_JuLeBu:
                setSelect(2);
                break;
            case R.id.id_tab_ShaiTu:
                setSelect(3);
                break;
            case R.id.id_tab_Wo:
                setSelect(4);
                break;
        }
    }
    @TargetApi(Build.VERSION_CODES.M)
    private void resetImgs(){
        mImgWo.setImageResource(R.drawable.ic_wo0);
        mImgShouye.setImageResource(R.drawable.ic_shouye0);
        mImgPeixun.setImageResource(R.drawable.ic_peixun0);
        mImgJulebu.setImageResource(R.drawable.ic_julebu0);
        mImgShaitu.setImageResource(R.drawable.ic_shaitu0);

        Textpeixun.setTextColor(getColor(R.color.bai));
        Textjulebu.setTextColor(getColor(R.color.bai));
        Textshaitu.setTextColor(getColor(R.color.bai));
        Textwo.setTextColor(getColor(R.color.bai));
    }
}
